/**
 * Copyright (C) 2008 Christopher Allen Ogden
 *
 *     This file is part of Odysi.
 *
 *  Odysi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Odysi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Odysi.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <string>
#include <vector>

#ifndef SERVER
    #include "image.h"
#endif

/**
 * Stores a tile.
 *
 * @author  Ogden, Chris
 */
class Tile
{
    public:
        /**
         * Constructor.
         */
        Tile():
            _walkable(0),
            _ticksPerFrame(1),
            _height(0) { }

        /**
         * Add a frame of animation.
         *
         * @param fileName  The file name of the frame.
         * @param xOff      The X offset of the frame.
         * @param yOff      The Y offset of the frame.
         */
        void addFrame(const std::string &fileName, int xOff, int yOff)
        {
#ifndef SERVER
            Image image;
            image.load(fileName);
            _image.push_back(image);
            _xOff.push_back(xOff);
            _yOff.push_back(yOff);
#endif
        }

        enum {UP = 1, RIGHT = 2, DOWN = 4, LEFT = 8};    

        /**
         * Draw the animation.
         *
         * @param frame The frame to draw.
         * @param x     The X location to draw to.
         * @param y     The Y location to draw to.
         */
        inline void draw(unsigned int frame, int x, int y) const
        {
#ifndef SERVER
            frame /= _ticksPerFrame;
            if(_image.empty()) return;
            frame = frame % _image.size();
            _image[frame].draw(x + _xOff[frame], y + _yOff[frame]);
#endif
        }

        /**
         * Get the bitmask for the tile.
         *
         * @return  The tile's bitmask.
         */
        inline int getWalkable(void) const
        {
            return _walkable;
        }

        /**
         * Set the bitmask for the tile.
         *
         * @param walkable  The bitmask.
         */
        inline void setWalkable(int walkable)
        {
            _walkable = walkable;
        }

        /**
         * Get the number of tickers per frame.
         *
         * @return  The number of ticks per frame.
         */
        inline unsigned int getTicksPerFrame(void) const
        {
            return _ticksPerFrame;
        }

        /**
         * Set the number of ticks per frame.
         *
         * @param ticks The number of ticks per frame.
         */
        inline void setTicksPerFrame(unsigned int ticks)
        {
            _ticksPerFrame = ticks;
        }

        /**
         * Set the height of the tile.
         *
         * @param height    The height of the tile.
         */
        inline void setHeight(unsigned int height)
        {
            _height = height;
        }

        /**
         * Get the height of the tile.
         *
         * @return  The height.
         */
        inline unsigned int getHeight(void) const
        {
            return _height;
        }

    private:
#ifndef SERVER
        typedef std::vector<Image> ImageList;
#endif

        std::vector<int> _xOff;
        std::vector<int> _yOff;
#ifndef SERVER
        ImageList _image;
#endif
        int _walkable;
        unsigned int _ticksPerFrame;
        unsigned int _height;
};

